package com.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.entity.SportRecord;
import com.web.mapper.SportRecordMapper;
import com.web.service.SportRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SportRecordServiceImpl implements SportRecordService {

    @Autowired
    private SportRecordMapper sportRecordMapper;

    @Override
    public List<SportRecord> getByUserIdAndDate(SportRecord sportRecord) {
        return sportRecordMapper.getByUserIdAndDate(sportRecord);
    }

    public PageInfo<SportRecord> list(Integer page, Integer pageSize, SportRecord sportRecord){
        PageHelper.startPage(page, pageSize);
        PageInfo<SportRecord> pageInfo = new PageInfo<>(sportRecordMapper.list(sportRecord));
        return pageInfo;
    }

    public SportRecord findById(Integer sportId){
        return sportRecordMapper.findById(sportId);
    }

    public void add(SportRecord sportRecord){
        sportRecord.setCreateTime(LocalDateTime.now());
        sportRecord.setUpdateTime(LocalDateTime.now());
        sportRecord.setRecordDate(LocalDate.now());
        sportRecordMapper.add(sportRecord);
    }

    public void update(Integer sportId,SportRecord sportRecord){
        sportRecord.setUpdateTime(LocalDateTime.now());
        sportRecord.setRecordDate(LocalDate.now());
        sportRecord.setSportId(sportId);
        sportRecordMapper.update(sportRecord);
    }

    public void delete(Integer sportId){
        sportRecordMapper.delete(sportId);
    }

    public PageInfo<SportRecord> byUserId(Integer page, Integer pageSize, Integer userId){
        PageHelper.startPage(page, pageSize);
        PageInfo<SportRecord> pageInfo = new PageInfo<>(sportRecordMapper.byUserId(userId));
        return pageInfo;
    }

    public PageInfo<SportRecord> byDoctorId(Integer page, Integer pageSize, Integer doctorId){
        PageHelper.startPage(page, pageSize);
        PageInfo<SportRecord> pageInfo = new PageInfo<>(sportRecordMapper.byDoctorId(doctorId));
        return pageInfo;
    }
}
